package com.develope.tree.image;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.develope.tree.R;

public class FullScreenImageAdapter extends PagerAdapter {

	private int width,heigh;
	private Activity _activity;
	private ArrayList<String> _imagePaths;
	private LayoutInflater inflater;

	public FullScreenImageAdapter(Activity activity,
			ArrayList<String> imagePaths, int width, int heigh) {
		this._activity = activity;
		this._imagePaths = imagePaths;
		this.width = width;
		this.heigh = heigh;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this._imagePaths.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		// TODO Auto-generated method stub
		return view == ((RelativeLayout) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageView imgDisplay;
		Button btnClose;
		

		inflater = (LayoutInflater) _activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image,
				container, false);

		imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
		btnClose = (Button) viewLayout.findViewById(R.id.btnClose);

		int targetW = 720;
		int targetH = 720;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(_imagePaths.get(position),options);
		
		int photoW = options.outWidth;
		int photoH = options.outHeight;
		
		int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
		
		options.inJustDecodeBounds = false;
		options.inSampleSize = scaleFactor;
		options.inPurgeable = true;
		
		Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
		imgDisplay.setImageBitmap(bitmap);

		btnClose.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_activity.finish();
			}
		});

		
		((ViewPager) container).addView(viewLayout);

		return viewLayout;

	}

	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((RelativeLayout) object);

	}
}
